package com.china08.yxyapi.api.schwebsite.album;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.entity.mg.sws.SwsAlbums;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;
import com.china08.yxyapi.repository.mg.SwsAlbumsImageRepository;
import com.china08.yxyapi.repository.mg.YxySwsAlbumsRepository;
import com.china08.yxyapi.util.ThumborUtil;

@CrossOrigin
@RestController("SwsAlbumListController")
public class SwsAlbumListController extends YxyApiBaseController {

    @Autowired
    private SwsAlbumsImageRepository swsAlbumsImageRepository;

    @Autowired
    private YxySwsAlbumsRepository yxySwsAlbumsRepository;

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @RequestMapping(value = "/api_yw/schwebsite/album", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String domainName = getDomainName(request);
        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
        // 通过域名获取学校ID
        SwsSettingDomains byDomainName = swsSettingDomainsRepository.findByDomainName(domainName);
        Assert.isTrue(null != byDomainName, "域名对象为空");

        String schoolId = byDomainName.getSchoolId();
        Pageable pageable = reqModel.getPageable(Direction.DESC, "albumsDate");
        Integer imageCount = 0;
        Page<SwsAlbums> pageSwsAlbums = yxySwsAlbumsRepository.findBySchoolIdAndImageCountNot(schoolId, imageCount,
                pageable);
        Assert.isTrue(null != pageSwsAlbums, "相册为空");
        List<RespModel> list = new ArrayList<RespModel>();
        for (SwsAlbums swsAlbums : pageSwsAlbums) {
            RespModel model = new RespModel();
            model.id = swsAlbums.getId();
            model.covers = swsAlbums.getCovers();
            if (StringUtils.isBlank(swsAlbums.getDescription())) {
                model.description = "暂无简介";
            } else {
                model.description = swsAlbums.getDescription();
            }
            long albumNum = swsAlbumsImageRepository.countByAlbumId(swsAlbums.getId());
            model.albumNum = albumNum;
            model.name = swsAlbums.getName();
            model.schoolId = swsAlbums.getSchoolId();
            if (StringUtils.isNotBlank(swsAlbums.getCovers())) {
                model.covers_200_130 = ThumborUtil.schoolWebSiteResize(swsAlbums.getCovers(), 200, 130);
                model.covers_188_152 = ThumborUtil.schoolWebSiteResize(swsAlbums.getCovers(), 188, 152);
            } else {
                model.covers_200_130 = "";
                model.covers_188_152 = "";
            }

            list.add(model);
        }
        Page<RespModel> pageRes = new PageImpl<RespModel>(list, pageable, pageSwsAlbums.getTotalElements());

        return pageRes;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

    }

    protected static class RespModel extends YxyApiRespModel {

        public String id;

        public String schoolId;

        public String name;

        public String description;

        public String covers;

        public String covers_200_130;

        public String covers_188_152;

        public Long albumNum;
    }
}
